package com.chenyi.web.controller.system;

import com.chenyi.common.annotation.Log;
import com.chenyi.common.core.controller.BaseController;
import com.chenyi.common.core.domain.entity.SysDictData;
import com.chenyi.common.core.page.TableDataInfo;
import com.chenyi.common.enums.BusinessType;
import com.chenyi.common.utils.AdminUtils;
import com.chenyi.common.utils.poi.ExcelUtil;
import com.chenyi.system.service.ISysDictDataService;
import com.chenyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 * 
 * @author wangkh
 */
@RestController
@RequestMapping("/admin/dict/data")
public class AdminDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @PreAuthorize("@admin.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public ResponseEntity<TableDataInfo> list(SysDictData dictData)
    {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return new ResponseEntity<>(getDataTable(list),HttpStatus.OK);
    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@admin.hasPermi('system:dict:export')")
    @GetMapping("/export")
    public ResponseEntity<Object> export(SysDictData dictData)
    {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        return util.exportExcel(list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@admin.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public ResponseEntity<Object> getInfo(@PathVariable Long dictCode)
    {
        return ResponseEntity.ok(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public ResponseEntity<Object> dictType(@PathVariable String dictType)
    {
        return ResponseEntity.ok(dictTypeService.selectDictDataByType(dictType));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@admin.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Object> add(@Validated @RequestBody SysDictData dict)
    {
        dict.setCreateBy(AdminUtils.getUsername());
        dictDataService.insertDictData(dict);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@admin.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Object> edit(@Validated @RequestBody SysDictData dict)
    {
        dict.setUpdateBy(AdminUtils.getUsername());
        dictDataService.updateDictData(dict);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@admin.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public ResponseEntity<Object> remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
